package noname;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.opensymphony.xwork2.ActionSupport;

public class Profile extends ActionSupport {
    private String id;
    private String id_user;
    private String about;
    private String logo;
    private String name;

    public String profile() throws ClassNotFoundException, SQLException {

	String q_profile = "select * from profile where id = " + getId();
	String q_user = "select * from users where id = ?";
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();
	Statement st = conn.createStatement();
	PreparedStatement ps;
	st.executeQuery(q_profile);
	ResultSet rs = st.getResultSet();
	if (rs.next()) {
	    ps = conn.prepareStatement(q_user);

	    this.id = rs.getString(1);
	    this.id_user = rs.getString(2);
	    this.about = rs.getString(3);
	    this.logo = rs.getString(4);
	    ps.setString(1, id_user);
	    ps.executeQuery();
	    rs = ps.getResultSet();
	    rs.next();
	    this.name = rs.getString(3);
	    // this.name = "a";
	    // out.println(name);
	    // System.err.println(this.id);
	}
	return "success";
    }

    public String save() throws ClassNotFoundException, SQLException {

	String insert = "update profile set about=?, logo=? where id_user = ?";

	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();

	String about = getAbout();

	String logo = new String("");
	logo = getLogo();

	String id_user = getId();

	PreparedStatement ps;
	ps = conn.prepareStatement(insert);
	ps.setString(1, about);
	if (logo == null)
	    ps.setString(2, "");
	else
	    ps.setString(2, logo);
	ps.setString(3, id_user);
	ps.execute();
	this.id = getId();
	setId(getId());
	setId("1");
	System.err.println(getId());
	setId("1");
	return "success";
    }

    public void setId(String id) {

	this.id = id;

    }

    public String getId() {
	return this.id;
    }

    public void setName(String name) {

	this.name = name;

    }

    public String getName() {
	return this.name;
    }

    public void setAbout(String about) {

	this.about = about;

    }

    public String getAbout() {
	return this.about;
    }

    public void setLogo(String logo) {

	this.logo = logo;

    }

    public String getLogo() {
	return this.logo;
    }

    public String getIdUser2() {
	return this.id_user;
    }

    public void setIdUser2(String id_user) {

	this.id_user = id_user;

    }

}
